package com.fesine.quartz.chapter9;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.rmi.RMISecurityManager;
import java.util.Date;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2016/11/20 15:49
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2016/11/20 15:49
 */
public class QuartzRMIServer {
    public void run() throws Exception {
        Log logger = LogFactory.getLog(QuartzRMIServer.class);
        //使用自定义的properties文件
        System.setProperty("org.quartz.properties","server.properties");
        //使用案例管理策略
        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new RMISecurityManager());
        }
        //获取scheduler实例
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.start();
        logger.info("Quartz RMI Server started at " + new Date());
        logger.info("RMI Clients may now access it. ");
        System.out.println("\n");
        System.out.println( "The scheduler will run until you type \"exit\"");
        BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            System.out.print("Type 'exit' to shutdown server: ");
            if ("exit".equals(bfr.readLine())) {
                break;
            }
            logger.info("Scheduler is shutting down...");
            scheduler.shutdown(true);
            logger.info("Scheduler has been stopped.");
        }
    }

    public static void main(String[] args) throws Exception{
            new QuartzRMIServer().run();
    }
}
